import { request } from '@/utils/request';

const Api = {
  SiteCategory: '/site/group',
  SiteList: '/site/list',
  SiteInfo: '/site/info',
  SiteExport: '/site/export',
  SiteDelete: '/site/delete',
  SiteSave: '/site/save',
  SiteSearch: '/site/search',
  SiteBooInfo: '/site/bookinfo',
  SiteChapter: '/site/chapter',
  SiteContent: '/site/content',
  SiteRequestCancel: '/site/cancelRequest',
  DebugClear: '/debug/clear',
};

/**
 * 获取源分类列表
 */
export function ApiSiteCategoryList() {
  return request.post({
    url: Api.SiteCategory,
  });
}

/**
 * 获取源列表
 * @param params
 */
export function ApiSiteList(params?: any) {
  return request.post({
    url: Api.SiteList,
    params,
  });
}

/**
 * 获取源信息
 * @param id
 */
export function ApiSiteInfo(id: any) {
  return request.post({
    url: Api.SiteInfo,
    params: {
      id,
    },
  });
}

/**
 * 导出源
 * @param id
 */
export function ApiSiteExport(id: any) {
  return request.post({
    url: Api.SiteExport,
    params: {
      id,
    },
  });
}

/**
 * 删除源
 * @param id
 */
export function ApiSiteDelete(id: any) {
  return request.post({
    url: Api.SiteDelete,
    params: {
      id,
    },
  });
}

/**
 * 保存到手机
 *
 * @param id
 * @param siteJson
 * @param params
 * @constructor
 */
export function ApiSiteSave(id: any, siteJson: string, params: object) {
  return request.post({
    url: Api.SiteSave,
    params: {
      id,
      siteJson,
      ...params,
    },
  });
}

/**
 * 取消请求
 * @param type 0搜索1章节2正文
 * @constructor
 */
export function ApiSiteRequestCancel(type: any) {
  return request.post({
    url: Api.SiteRequestCancel,
    params: {
      type,
    },
  });
}

/**
 * 搜索测试
 *
 * @param keyword
 * @param siteJson
 * @constructor
 */
export function ApiSiteSearch(keyword: string, siteJson: string) {
  // 取消请求
  const searchAbort = new AbortController();
  const searchRequest = request.post({
    url: Api.SiteSearch,
    params: {
      keyword,
      siteJson,
    },
    signal: searchAbort.signal,
  });
  return { searchRequest, searchAbort };
}

/**
 * 详情测试
 * @param bookJson
 * @param siteJson
 * @constructor
 */
export function ApiSiteBookInfo(bookJson: string, siteJson: string) {
  // 取消请求
  const bookInfoAbort = new AbortController();
  const bookInfoRequest = request.post({
    url: Api.SiteBooInfo,
    params: {
      bookJson,
      siteJson,
    },
    signal: bookInfoAbort.signal,
  });
  return { bookInfoRequest, bookInfoAbort };
}

/**
 * 章节测试
 *
 * @param bookJson
 * @param siteJson
 * @constructor
 */
export function ApiSiteChapter(bookJson: string, siteJson: string) {
  // 取消请求
  const chapterAbort = new AbortController();
  const chapterRequest = request.post({
    url: Api.SiteChapter,
    params: {
      bookJson,
      siteJson,
    },
    signal: chapterAbort.signal,
  });
  return { chapterRequest, chapterAbort };
}


/**
 * 正文测试
 *
 * @param catalogJson
 * @param siteJson
 * @constructor
 */
export function ApiSiteContent(catalogJson: string, siteJson: string) {
  // 取消请求
  const contentAbort = new AbortController();
  const contentRequest = request.post({
    url: Api.SiteContent,
    params: {
      catalogJson,
      siteJson,
    },
    signal: contentAbort.signal,
  });
  return { contentRequest, contentAbort };
}

/**
 * 清除缓存
 * @param type
 * @constructor
 */
export function ApiDebugClear(type: any) {
  return request.post({
    url: Api.DebugClear,
    params: {
      type,
    },
  });
}
